<?php
namespace Home\Controller;
use Think\Controller;

class HyzsController extends Controller {
    public function index(){
        indexRvent();//测算首页执行事件
        $this->assign('titleName',titleName('Hyzs',1));//首页名称
        cookie('orderidnum','Hyzs');

        indexRequest($_REQUEST);//测算首页接收数据处理

        cnzz_pvuv('HY');//执行统计

        $channel=cookie('channel');
        if($channel=='qudao215'){
            header('location:https://www.yixueqm.com/zhiming/index.php/Home-Lath-index');
        }

        //渠道获取ID
        $channelID=obtain_channelID('HY');
        $this->assign('channelID',$channelID);

        $user_agent = $_SERVER['HTTP_USER_AGENT'];//用户使用的浏览器，操作系统等信息。
        if (strpos($user_agent, 'MicroMessenger') == false) {
            //非微信浏览器访问
            cookie('wxlogin',0);
        }else{
            cookie('wxlogin',1);

            if($_SERVER['HTTP_HOST']=='hy.yixueqm.com'){
                if($_REQUEST['openid']!=''){
                    cookie('openid',$_REQUEST['openid'],7100);
                    S('ticket',$_REQUEST['ticket'],7100);
                }
                $openid=cookie('openid');
                $wzurl=wxJsdkData();
                if(empty($openid)){
                    openidJump('Hyzs',cookie('channel'));
                }

                $noncestr=noncestr(15);
                $timestamp=time();
                $ticket= S('ticket');

                $signature=sha1("jsapi_ticket={$ticket}&noncestr={$noncestr}&timestamp={$timestamp}&url={$wzurl}");

                $this->assign('appid',cookie('APPID'));
                $this->assign('timestamp',$timestamp);
                $this->assign('noncestr',$noncestr);
                $this->assign('signature',$signature);
            }else{
                getToken();
                getTicket();
                $noncestr=noncestr(15);
                $timestamp=time();
                $ticket= S('ticket');
                $wzurl=wxJsdkData();
                $signature=sha1("jsapi_ticket={$ticket}&noncestr={$noncestr}&timestamp={$timestamp}&url={$wzurl}");

                $this->assign('appid',cookie('APPID'));
                $this->assign('timestamp',$timestamp);
                $this->assign('noncestr',$noncestr);
                $this->assign('signature',$signature);

                if(!empty($_REQUEST['code'])){
                    cookie('code',$_REQUEST['code'],7100);
                }
                getCode($wzurl);
                getOopenid();
            }
            wxShareUrl('HY','Hyzs');//分享地址获取
            cookie('uid',cookie('openid'));
        }


        //婚缘走势随机数
        csRandNum('hyzscs',300000,499999);

        //半价
        $jieguoye=$_REQUEST['jieguoye'];
        banjia($jieguoye);

        $this->assign('orderidnum',cookie('orderidnum'));

        if(channelVivo($channel)){
            $this->display("Hyzs/ZM_hunyuanzoushi_index_vivo");
        }else{
            $this->display("Hyzs/ZM_hunyuanzoushi_index");
        }

    }
    public function hyzs_zhifu(){
        $this->assign('titleName',titleName('Hyzs',2,'HY'));//首页名称

        cookie('timeStrto',time()+7200);//倒计时时间戳
        $user_agent = $_SERVER['HTTP_USER_AGENT'];//用户使用的浏览器，操作系统等信息。
        if (strpos($user_agent, 'MicroMessenger') == false) {
            //非微信浏览器访问
            cookie('wxlogin',0);
        }else{
            cookie('wxlogin',1);

            if($_SERVER['HTTP_HOST']=='hy.yixueqm.com'){

            }else{
                getToken();
                getTicket();
            }

            $noncestr=noncestr(15);
            $timestamp=time();
            $ticket= S('ticket');
            $wzurl=wxJsdkData();
            $signature=sha1("jsapi_ticket={$ticket}&noncestr={$noncestr}&timestamp={$timestamp}&url={$wzurl}");

            $this->assign('appid',cookie('APPID'));
            $this->assign('timestamp',$timestamp);
            $this->assign('noncestr',$noncestr);
            $this->assign('signature',$signature);
        }
        if($_REQUEST['birthday']==''){
            $birthday='0-2018-08-08-8';
            cookie('zymd',$birthday);
        }


        if($_REQUEST['username']){//返回操作
            cookie('znickname',I('post.username'));
            cookie('zymd',mb_substr($_REQUEST['birthday'],2,10));
            cookie('zhour',mb_substr($_REQUEST['birthday'],13,2));
            cookie('datetype',mb_substr($_REQUEST['birthday'],0,1));

            cookie('fymd',mb_substr($_REQUEST['girl_birthday'],2,10));
            cookie('fhour',mb_substr($_REQUEST['girl_birthday'],13,2));
            cookie('fnickname',I("post.girl_username"));
            cookie('fdatetype',mb_substr($_REQUEST['girl_birthday'],0,1));
        }
        if(!empty($_REQUEST['ordernum'])){//未支付订单
            $arrdata[0]=S('sm_h5test|'.$_REQUEST['ordernum']);
            $zymd=$arrdata[0]['year'].'-'.str_pad($arrdata[0]['month'],2,"0",STR_PAD_LEFT).'-'.str_pad($arrdata[0]['day'],2,'0',STR_PAD_LEFT);
            cookie('zymd',$zymd,604800);
            cookie('zhour',$arrdata[0]['hour'],604800);
            cookie('znickname',$arrdata[0]['username'],604800);
            cookie('datetype',$arrdata[0]['datetype']);

            $farrdata=S('sm_h5test|'.$_REQUEST['ordernum'].'_');
            cookie('fymd',$farrdata[0]['year'].'-'.str_pad($farrdata[0]['month'],2,"0",STR_PAD_LEFT).'-'.str_pad($farrdata[0]['day'],2,"0",STR_PAD_LEFT),604800);
            cookie('fhour',$farrdata[0]['hour'],604800);
            cookie('fnickname',$farrdata[0]['username'],604800);
            cookie('fdatetype',$arrdata[0]['datetype']);

            cookie('orderid',$_REQUEST['ordernum'],604800);
            cookie('orderidnum','Hyzs',604800);
        }
        $y=mb_substr(cookie('zymd'),0,4);//年
        $m=mb_substr(cookie('zymd'),5,2); //月
        $d=mb_substr(cookie('zymd'),8,2);//日
        $zyinli=yinli($y,$m,$d);
        cookie('zyangli',$y.'年'.$m.'月'.$d.'日'.' '.cookie('zhour').'时',604800);//阳历
        cookie('zyinli',$zyinli[0].'年'.$zyinli[1].$zyinli[2],604800);//阴历

        $fy=mb_substr(cookie('fymd'),0,4);//年
        $fm=mb_substr(cookie('fymd'),5,2); //月
        $fd=mb_substr(cookie('fymd'),8,2);//日
        $fyinli=yinli($fy,$fm,$fd);
        cookie('fyangli',$fy.'年'.$fm.'月'.$fd.'日'.' '.cookie('fhour').'时',604800);//阳历
        cookie('fyinli',$fyinli[0].'年'.$fyinli[1].$fyinli[2],604800);//阴历

        $data=cookie('orderid');//获取订单号
        if(empty($data)&&empty($_REQUEST['ordernum'])){
            $orderid='HY'.date('Ymd').mt_rand(100,999).date('His');
            cookie('orderid',$orderid,604800);
        }

        //创建订单
        $hour=cookie('zhour');if($hour==''){$hour=1;}
        $fhour=cookie('fhour');if($fhour==''){$hour=1;}
        $shop=cookie('orderid');
        $znickname=cookie('znickname');
        $fnickname=cookie('fnickname');
        $datetype=cookie('datetype');if($datetype=='')$datetype=0;
        $fdatetype=cookie('fdatetype');if($fdatetype=='')$fdatetype=0;
        $channel=cookie('channel');
        $imei=cookie('uid');if(cookie('uid')==''){$imei=cookie('imei');}
        defaultPrice();

        channelPay($channel,'HY');//查询渠道价格

        //半价
//        if(cookie('banjia')==1){
//            $price=intval(cookie('price')/2);
//            cookie('price',$price);
//        }

        if(cookie('couponPrice')){//是否有使用优惠券
            cookie('price',cookie('couponPrice'));
            $this->assign('discount',10);//优惠券价格
        }else{
            $this->assign('discount',0);//优惠券价格
        }

        csPay($znickname,$channel);//测试价格判断

        $price=cookie('price');
        if(empty($_REQUEST['ordernum'])){
//            $arrdata=M()->query("select * from sm_h5test where ordernum='{$shop}'");
//            if(empty($arrdata)){
//                M()->query("insert into sm_h5test (ordernum,price,username,typeid,sex,status,datetype,year,month,day,hour,paykind,date_remark,appuserid,ip,channel)values(
//                                              '{$shop}','{$price}','{$znickname}',16,1,-1,{$datetype},{$y},{$m},{$d},'{$hour}',-1,'{$imei}','{$imei}','{$_SERVER['REMOTE_ADDR']}','{$channel}')");
//                M()->query("insert into sm_h5test (ordernum,price,username,typeid,sex,status,datetype,year,month,day,hour,paykind,date_remark,ip)values(
//                                              '{$shop}_',0,'{$fnickname}',16,0,-1,{$datetype},{$fy},{$fm},{$fd},'{$fhour}',-1,'{$imei}','{$_SERVER['REMOTE_ADDR']}')");
//                $IndexController=A('Index');
//                $IndexController->subNumber('HY');
//            }
//            if(!cookie($_REQUEST['username'])){//返回操作
//                //更新订单
//                M()->query("update sm_h5test set username='{$znickname}',year={$y},month={$m},day={$d} where ordernum='{$shop}'");
//                M()->query("update sm_h5test set username='{$fnickname}',year={$fy},month={$fm},day={$fd} where ordernum='{$shop}_'");
//            }
            $arrdata=S('sm_h5test|'.$shop);
            $Farrdata=S('sm_h5test|'.$shop.'_');
            if(empty($arrdata)){
                $smH5testinter=array(
                    'ordernum'=>$shop, 'price'=>$price, 'username'=>$znickname, 'typeid'=>16, 'sex'=>1, 'createtime'=>date('Y-m-d H:i:m'), 'status'=>-1,
                    'datetype'=>$datetype, 'year'=>$y, 'month'=>$m, 'day'=>$d, 'hour'=>$hour, 'appuserid'=>$imei, 'ip'=>$_SERVER['REMOTE_ADDR'],
                    'channel'=>$channel, 'auto_send_sms'=>0, 'place'=>0, 'paykind'=>-1
                );
                $FsmH5testinter=array(
                    'ordernum'=>$shop.'_', 'price'=>0, 'username'=>$fnickname, 'typeid'=>16, 'sex'=>0, 'createtime'=>date('Y-m-d H:i:m'), 'status'=>-1,
                    'datetype'=>$fdatetype, 'year'=>$fy, 'month'=>$fm, 'day'=>$fd, 'hour'=>$fhour, 'appuserid'=>$imei, 'ip'=>$_SERVER['REMOTE_ADDR'],
                    'channel'=>$channel, 'auto_send_sms'=>0, 'place'=>0, 'paykind'=>-1
                );
                S('sm_h5test|'.$shop,$smH5testinter,2592000);//2592000为30天
                cacheZhiFuOrderid($shop);//支付页缓存订单操作
                S('sm_h5test|'.$shop.'_',$FsmH5testinter,2592000);//2592000为30天
                cacheZhiFuOrderid($shop.'_');//支付页缓存订单操作

                $IndexController=A('Index');
                $IndexController->subNumber('HY');
            }else if(!cookie($_REQUEST['username'])) {//返回操作
                //更新订单
                $arrdata['username']=$znickname;$arrdata['datetype']=$datetype;$arrdata['year']=$y;$arrdata['month']=$m;$arrdata['day']=$d;
                S('sm_h5test|'.$shop,$arrdata,2592000);//2592000为30天
                $Farrdata['username']=$fnickname;$Farrdata['datetype']=$fdatetype;$Farrdata['year']=$fy;$Farrdata['month']=$fm;$Farrdata['day']=$fd;
                S('sm_h5test|'.$shop.'_',$Farrdata,2592000);//2592000为30天
            }
        }


        zwmp(cookie('zymd'), cookie('zhour'),'男','sizhu');
        $sizhu=I('globals.sizhu');
        $this->assign('sizhu',$sizhu);//Z四柱
        if($datetype==1){
            $this->assign('datetype',"农历 ".cookie('zyinli')." ".mb_substr($sizhu[3],1,1,'utf-8')."时");
        }else{
            $this->assign('datetype',"阳历 ".cookie('zyangli'));
        }

        zwmp(cookie('fymd'), cookie('fhour'),'女','sizhu');
        $sizhu=I('globals.sizhu');
        $this->assign('fsizhu',$sizhu);//Z四柱
        if($datetype==1){
            $this->assign('fdatetype',"农历 ".cookie('fyinli')." ".mb_substr($sizhu[3],1,1,'utf-8')."时");
        }else{
            $this->assign('fdatetype',"阳历 ".cookie('fyangli'));
        }


        if (strpos($user_agent, 'MicroMessenger') == false) {
            //非微信浏览器访问
            if (strpos($user_agent, 'Windows') == true||strpos($user_agent,'Mac') == true) {
                //Windows 电脑访问
                if(channelSaoma($channel)){
                    $this->assign('pc',1);
                    $saomaUrl=wxsaoma('HY','婚缘走势',cookie('orderid'));//微信扫码回调参数
                    $this->assign('wxsaoma',$saomaUrl);
                }
            }
        }else{
            $xml=wxgzhzf('HY','婚缘走势',cookie('orderid'));//公众号支付回调参数
            $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
            $val = json_decode(json_encode($xmlstring),true);
            $noncestr=noncestr(15);//随机字符串
            $timestamp=time();//时间戳

            $stringA="appId=".cookie('APPID')."&nonceStr={$noncestr}&package=prepay_id={$val['prepay_id']}&signType=MD5&timeStamp={$timestamp}";
            $stringSignTemp=$stringA."&key=".cookie('wxKEY');
            $sign=strtoupper(md5($stringSignTemp));

            $this->assign('timeStamp',$timestamp);
            $this->assign('nonceStr',$noncestr);
            $this->assign('package','prepay_id='.$val['prepay_id']);
            $this->assign('signType','MD5');
            $this->assign('paySign',$sign);
            $this->assign('appId',cookie('APPID'));
        }

        $this->assign('csNameJump',U('Hyzs/jieguoye','',false).'?ordernum='.cookie('orderid'));//支付成功跳转地址

        $this->assign('channel',cookie('channel'));
        $this->assign('znickname',cookie('znickname'));
        $this->assign('fnickname',cookie('fnickname'));
        $this->assign('wxlogin',cookie('wxlogin'));
        $this->assign('orderidnum',cookie('orderidnum'));


        if(channelVivo($channel)){
            $this->display("Hyzs/ZM_hunyuanzoushi_zhifu_vivo");
        }else{
            $this->display("Hyzs/ZM_hunyuanzoushi_zhifu");
        }

    }
    public function jieguoye(){
        jieguoInterface('Hyzs');//结果页公共函数

        $channel=cookie('channel');
        $this->assign('titleName',titleName('Hyzs',3));//首页名称
        $user_agent = $_SERVER['HTTP_USER_AGENT'];//用户使用的浏览器，操作系统等信息。
        if (strpos($user_agent, 'MicroMessenger') == false) {
            //非微信浏览器访问
            cookie('wxlogin',0);
        }else{
            cookie('wxlogin',1);
            $this->assign('wxlogin',1);
            if($_SERVER['HTTP_HOST']==cookie('jieguoUrlYM')){

            }else{
                getToken();
                getTicket();
            }
            $noncestr=noncestr(15);
            $timestamp=time();
            $ticket= S('ticket');
            $wzurl=wxJsdkData();
            $signature=sha1("jsapi_ticket={$ticket}&noncestr={$noncestr}&timestamp={$timestamp}&url={$wzurl}");

            $this->assign('appid',cookie('APPID'));
            $this->assign('timestamp',$timestamp);
            $this->assign('noncestr',$noncestr);
            $this->assign('signature',$signature);
        }

        if(!empty($_REQUEST['ordernum'])){
            $ordernum=$_REQUEST['ordernum'];
            $arrdata[0]=jieguoyeArrData($ordernum);

            $zymd=$arrdata[0]['year'].'-'.str_pad($arrdata[0]['month'],2,"0",STR_PAD_LEFT).'-'.str_pad($arrdata[0]['day'],2,'0',STR_PAD_LEFT);
            cookie('zymd',$zymd,604800);
            $zhour=$arrdata[0]['hour'];
            cookie('zhour',$zhour,604800);
            cookie('znickname',$arrdata[0]['username'],604800);

            $farrdata[0]=S('sm_h5test|'.$ordernum.'_');
            if($farrdata[0]['year']==''){
                $farrdata=M()->query("select * from sm_h5test where ordernum='{$_REQUEST['ordernum']}_'");
            }

            cookie('fymd',$farrdata[0]['year'].'-'.str_pad($farrdata[0]['month'],2,"0",STR_PAD_LEFT).'-'.str_pad($farrdata[0]['day'],2,"0",STR_PAD_LEFT),604800);
            cookie('fhour',$farrdata[0]['hour'],604800);
            cookie('fnickname',$farrdata[0]['username'],604800);

            $y=mb_substr(cookie('zymd'),0,4);//年
            $m=mb_substr(cookie('zymd'),5,2); //月
            $d=mb_substr(cookie('zymd'),8,2);//日
            $zyinli=yinli($y,$m,$d);
            cookie('zyangli',$y.'年'.$m.'月'.$d.'日'.' '.cookie('zhour').'时',604800);//阳历
            cookie('zyinli',$zyinli[0].'年'.$zyinli[1].$zyinli[2],604800);//阴历

            $fy=mb_substr(cookie('fymd'),0,4);//年
            $fm=mb_substr(cookie('fymd'),5,2); //月
            $fd=mb_substr(cookie('fymd'),8,2);//日
            $fyinli=yinli($fy,$fm,$fd);
            cookie('fyangli',$fy.'年'.$fm.'月'.$fd.'日'.' '.cookie('fhour').'时',604800);//阳历
            cookie('fyinli',$fyinli[0].'年'.$fyinli[1].$fyinli[2],604800);//阴历

            cookie('orderid',$arrdata[0]['ordernum'],604800);
        }

        //一对一亲算下单
        cookie('csTypePrice','288');//一对一价格
        $QSorder=new \Library\toolOrder();
        $wxzhifuArr=$QSorder->QSorderXD();
        if($wxzhifuArr==1){
            $this->assign('qsPay',1);
        }else{//更新数据hu
            if (strpos($user_agent, 'MicroMessenger') == false) {
                //非微信浏览器访问
            }else{
                $this->assign('timeStamp',$wxzhifuArr['timeStamp']);
                $this->assign('nonceStr',$wxzhifuArr['nonceStr']);
                $this->assign('package',$wxzhifuArr['package']);
                $this->assign('signType',$wxzhifuArr['signType']);
                $this->assign('paySign',$wxzhifuArr['paySign']);
                $this->assign('appId',$wxzhifuArr['appId']);
            }
        }


        if(cookie('zymd')!=''){
            zwmp(cookie('zymd'),cookie('zhour'),'男','zw');
            $this->assign('arryysz',I('globals.arryysz'));//四柱阴阳
            $zarryysz=I('globals.arryysz');
            $this->assign('sizhu',I('globals.sizhu'));//Z四柱
            $zsizhu=I('globals.sizhu');
            $ssarray=array();
            array_push($ssarray,implode(I('globals.nianzhuss')));
            array_push($ssarray,implode(I('globals.yuezhuss')));
            array_push($ssarray,implode(I('globals.shizhuss')));
            $this->assign('nianzhuss',$ssarray);//十神
            $this->assign('canggan',I('globals.canggan'));//藏干

            $this->assign('zhishen',I('globals.zhishen'));//支神
            $this->assign('sizhuny',I('globals.sizhuny'));//四柱纳音
            $zsizhuny=I('globals.sizhuny');
            $this->assign('bzdishi',I('globals.bzdishi'));//八字地势
            $this->assign('wxsqs',I('globals.wxsqs'));//旺相休囚死
            $this->assign('xiys',I('globals.xiys'));//喜用神
            $this->assign('taiyuan',I('globals.taiyuan'));//胎元
            $this->assign('rikong',I('globals.rikong'));//日空
            //$zsqkData=zsqk(I('globals.srg'),I('globals.arrzx'),I('globals.arrtf'),I('globals.arrjix'),I('globals.arrxiongx'),1);
            $arrzx=I('globals.arrzx');

            //自身状况
            $arrzx=array_values(array_filter($arrzx));
            $arrzhux=mb_substr($arrzx[0],0,2,'utf-8');
            $arrzhux=M()->query("select youdian,quedian from tb_zwds_zhuxing where star='{$arrzhux}' and palace='命宫'");
            $this->assign('zmgarrzhux',$arrzhux);//自身主星状况


//            $orderid=cookie('orderid');
//            $createtime=M()->query("select createtime from sm_h5test where ordernum='{$orderid}'");
//            $this->assign('createtime',$createtime[0]['createtime']);//创建时间
        }


        if(cookie('fymd')!=''){
            zwmp(cookie('fymd'),cookie('fhour'),'女','zw');
            $this->assign('arryysz',I('globals.arryysz'));//四柱阴阳
            $farryysz=I('globals.arryysz');
            $this->assign('fsizhu',I('globals.sizhu'));//Z四柱
            $fsizhu=I('globals.sizhu');
            $ssarray=array();
            array_push($ssarray,implode(I('globals.nianzhuss')));
            array_push($ssarray,implode(I('globals.yuezhuss')));
            array_push($ssarray,implode(I('globals.shizhuss')));
            $this->assign('fnianzhuss',$ssarray);//十神
            $this->assign('fcanggan',I('globals.canggan'));//藏干

            $this->assign('fzhishen',I('globals.zhishen'));//支神
            $this->assign('fsizhuny',I('globals.sizhuny'));//四柱纳音
            $fsizhuny=I('globals.sizhuny');
            $this->assign('fbzdishi',I('globals.bzdishi'));//八字地势
            $this->assign('fwxsqs',I('globals.wxsqs'));//旺相休囚死
            $this->assign('fxiys',I('globals.xiys'));//喜用神
            $this->assign('ftaiyuan',I('globals.taiyuan'));//胎元
            $this->assign('frikong',I('globals.rikong'));//日空
            //$zsqkData=zsqk(I('globals.srg'),I('globals.arrzx'),I('globals.arrtf'),I('globals.arrjix'),I('globals.arrxiongx'),1);
            $arrzx=I('globals.arrzx');
            //自身状况
            $arrzx=array_values(array_filter($arrzx));
            //2
            $arrzhux=array();
            foreach($arrzx as $key=>$value){
                if($key<3){
                    $zhux=mb_substr($value,0,2,'utf-8');
                    $arr=M()->query("select youdian,quedian from tb_zwds_zhuxing where star='{$zhux}' and palace='命宫'");
                    $arr[0]=array_filter($arr[0]);
                    $arrzhux=array_merge($arrzhux,$arr);
                }
            }
            $this->assign('fmgarrzhux',$arrzhux);//自身主星状况
        }

        $hunyuanzs=hunyuanzs($zsizhu,$fsizhu,mb_substr($zsizhuny[3],2,1,'utf-8'),mb_substr($fsizhuny[3],2,1,'utf-8'),$zarryysz[0],$farryysz[0]);
        $this->assign('arrdts',$hunyuanzs['arrdts']);//滴天髓
        $this->assign('arraqg',$hunyuanzs['arraqg']);//爱情观
        $this->assign('arrhyzs',$hunyuanzs['arrhyzs']);//婚恋方程 命盘点拨
        $this->assign('arrnum',$hunyuanzs['arrnum']);//幸福密码

        $this->assign('channel',cookie('channel'));
        $this->assign('orderidnum',cookie('orderidnum'));

        if(channelVivo($channel)){
            $this->display("Hyzs/ZM_hunyuanzoushi_jieguoye_vivo");
        }else{
            $this->display("Hyzs/ZM_hunyuanzoushi_jieguoye");
        }

    }
    public function pay(){//支付宝支付
        $this->assign('ordername1','婚缘走势');
        $this->assign('ordername2','知命-婚缘走势');
        $this->display("Index/pay");
    }
    public function zzhifu(){
        $data=M()->query("select status from sm_h5test where ordernum='".cookie('orderid')."'");
        $this->assign('status',$data[0]['status']);
        $this->display("Index/ZM_zzhifu");
    }
    public function paywx(){//H5微信支付
        H5ZMwxzhifu('HY','婚缘走势',cookie('orderid'),'Hyzs');//知命
    }

}